<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Agent page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript">

        // Initialize the producer select box
        function initCentreSelect() {
            removeAllSelectOptions("centre_code");
            addSelectOptions("centre_code", "Choose one", "centre");
            ajaxCall('getcentres.php', loadCentresFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadCentresFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);
            
            centre_list=xmlDoc.documentElement.getElementsByTagName("centre");
            for (i=0;i<centre_list.length;i++) {
                centre_code=centre_list[i].getElementsByTagName("code");
                centre_name=centre_list[i].getElementsByTagName("name");
                try {
                    code = centre_code[0].firstChild.nodeValue;
                    name = centre_name[0].firstChild.nodeValue;
                    addSelectOptions("centre_code", code, name);
                } catch (er) {
                    code = 1;
                    name = '';
                }
            }

        }

        function saveRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "added":
                    alert("Agent added!")
                    break;
                case "updated":
                    alert("Agent updated");
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the save response");
                    break;
            }
        }

        function queryRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "deleted":
                    alert('Deleted agent');
                    resetForm();
                    break;
                case "success":
                    //alert('Successful query');
                    try {
                        agent_name=xmlDoc.getElementsByTagName("agent_name");
                        document.getElementById("agent_name").value=agent_name[0].firstChild.nodeValue;
                        agent_code=xmlDoc.getElementsByTagName("agent_code");
                        document.getElementById("agent_code").value=agent_code[0].firstChild.nodeValue;
                        centre_code=xmlDoc.getElementsByTagName("centre_code");
                        selectOptionValue("centre_code", centre_code[0].firstChild.nodeValue);
                        mobile_phone=xmlDoc.getElementsByTagName("mobile_phone");
                        document.getElementById("mobile_phone").value=mobile_phone[0].firstChild.nodeValue;
                        office_phone=xmlDoc.getElementsByTagName("office_phone");
                        document.getElementById("office_phone").value=office_phone[0].firstChild.nodeValue;
                        address=xmlDoc.getElementsByTagName("address");
                        document.getElementById("address").value=address[0].firstChild.nodeValue;
                        password=xmlDoc.getElementsByTagName("password");
                        document.getElementById("password").value=password[0].firstChild.nodeValue;
                    } catch (er) {
                        alert('Invalid agent code');
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }
        }

        function dispTableHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            table_text = "<button onclick='displayTable()'>Display the agent table</button>";
            table_text = table_text + "<table border='1'>"+
                      "<tr><th>ID</th>"+
                          "<th>Agent Name</th>"+
                          "<th>Agent Code</th>"+
                          "<th>Centre Code</th>"+
                          "<th>Mobile Phone</th>"+
                          "<th>Office Phone</th>"+
                          "<th>Address</th>"+
                          "<th>Password</th></tr>";

            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    agents=xmlDoc.getElementsByTagName("agent");
                    for (i=0;i<agents.length;i++) {
                        table_text = table_text + "<tr>";
                        try {
                            table_text = table_text + insertField(agents[i], "agent_id");
                            table_text = table_text + insertField(agents[i], "agent_name");
                            table_text = table_text + insertField(agents[i], "agent_code");
                            table_text = table_text + insertField(agents[i], "centre_code");
                            table_text = table_text + insertField(agents[i], "mobile_phone");
                            table_text = table_text + insertField(agents[i], "office_phone");
                            table_text = table_text + insertField(agents[i], "address");
                            table_text = table_text + insertField(agents[i], "password");
                        } catch (er) {
                            alert('Invalid agent code');
                        }
                        table_text = table_text + "</tr>";
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }

            table_text = table_text + "</table>";
            document.getElementById("agent_table").innerHTML=table_text;
        }

        function validateForm() {
            var return_value = true;
            var agent_name=document.getElementById("agent_name").value;
            var agent_code=document.getElementById("agent_code").value;
            var centres=document.getElementById("centre_code");
            var mobile_phone=document.getElementById("mobile_phone").value;
            var office_phone=document.getElementById("office_phone").value;
            var address=document.getElementById("address").value;
            var password=document.getElementById("password").value;
            var password2=document.getElementById("password2").value;

            if (!agent_name) {
                document.getElementById("agent_name_hint").innerHTML =
                    "<p style='color:red'><b>Name cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("agent_name_hint").innerHTML = " ";
            }

            if (!agent_code) {
                document.getElementById("agent_code_hint").innerHTML =
                    "<p style='color:red'><b>Code cannot be empty</b></p>";
                return_value = false;
            } else {
                document.getElementById("agent_code_hint").innerHTML = " ";
            }

            if (!centres.selectedIndex) {
                document.getElementById("centre_code_hint").innerHTML =
                    "<p style='color:red'><b>Centre Code should be selected</b></p>";
                return_value = false;
            } else {
                document.getElementById("centre_code_hint").innerHTML = " ";
            }
            
            if (isNaN(mobile_phone) || !mobile_phone) {
                document.getElementById("mobile_phone_hint").innerHTML = 
                    "<p style='color:red'><b>Invalid phone number</b></p>";
                return_value = false;
            } else {
                document.getElementById("mobile_phone_hint").innerHTML = " ";
            }

            if (isNaN(office_phone) || !office_phone) {
                document.getElementById("office_phone_hint").innerHTML =
                    "<p style='color:red'><b>Invalid phone number</b></p>";
                return_value = false;
            } else {
                document.getElementById("office_phone_hint").innerHTML = " ";
            }

            if (!password) {
                document.getElementById("password_hint").innerHTML =
                    "<p style='color:red'><b>Password cannot be empty</b></p>";
                return_value = false;
            } else {
                document.getElementById("password_hint").innerHTML = " ";
            }

            if (password != password2) {
                document.getElementById("password2_hint").innerHTML =
                    "<p style='color:red'><b>Passwords do not match!</b></p>";
                return_value = false;
            } else {
                document.getElementById("password2_hint").innerHTML = " ";
            }

            return return_value;

        }

        function saveForm() {
            if (!validateForm()) {
                alert('Correct errors in the form before trying again!');
                return;
            }
            
            var agent_name=document.getElementById("agent_name").value;
            var agent_code=document.getElementById("agent_code").value;
            var centres=document.getElementById("centre_code");
            var selected_centre=centres.options[centres.selectedIndex].value;
            var mobile_phone=document.getElementById("mobile_phone").value;
            var office_phone=document.getElementById("office_phone").value;
            var address=document.getElementById("address").value;
            var password=document.getElementById("password").value;

            srvr_request="saveagent.php?agent_name="+agent_name+
                            "&agent_code="+agent_code+
                            "&centre_code="+selected_centre+
                            "&mobile_phone="+mobile_phone+
                            "&office_phone="+office_phone+
                            "&address="+address+
                            "&password="+password;
                        
            ajaxCall(srvr_request,saveRespHdlr);
        }

        function resetForm() {
            document.getElementById("agent_name").value = "";
            document.getElementById("agent_name_hint").innerHTML = "";
            document.getElementById("agent_code").value = "";
            document.getElementById("agent_code_hint").innerHTML = "";
            document.getElementById("mobile_phone").value = "";
            document.getElementById("mobile_phone_hint").innerHTML = "";
            document.getElementById("office_phone").value = "";
            document.getElementById("office_phone_hint").innerHTML = "";
            document.getElementById("address").value = "";
            document.getElementById("address_hint").innerHTML = "";
            document.getElementById("password").value = "";
            document.getElementById("password_hint").innerHTML = "";
            document.getElementById("password2").value = "";
            document.getElementById("password2_hint").innerHTML = "";
            initCentreSelect();
            document.getElementById("centre_code_hint").innerHTML = "";
        }

        function queryForm() {
            var agent_code=document.getElementById("agent_code").value;
            srvr_request="queryagent.php?agent_code="+agent_code;
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function deleteForm() {
            var agent_code=document.getElementById("agent_code").value;
            srvr_request="queryagent.php?agent_code="+agent_code+
                        "&action=delete";
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function displayTable() {
            srvr_request="queryagent.php";
            ajaxCall(srvr_request,dispTableHdlr);
        }

        function checkLoginCookie() {
            session_auth=getCookie('session_auth');
            logged_username=getCookie('username');

            if(session_auth!='kteam_auth' || !logged_username) {
                setCookie('redirector','agent.html',60*60*1000);
                window.location = "login.html";
            }
            initCentreSelect();
        }

    </script>

  </head>
  <body onLoad="checkLoginCookie()">

      <table border="1">
          <tr><td width="150"><a href="producer.html"><b>Producers</b></a></td>
              <td width="150"><a href="film.html"><b>Films</b></a></td>
              <td width="150"><a href="centre.html"><b>Centres</b></a></td>
              <td width="150"><a href="agent.html"><b>Agents</b></a></td>
              <td width="150"><a href="customer.html"><b>Tickets</b></a></td>
              <td width="150"><a href="ticket.html"><b>Ticket rate information</b></a></td>
              <td width="150"><a href="ticketstatus.html"><b>Status of ticket sales for each film</b></a></td>
              <td width="150"><a href="customerstatus.html"><b>Status for each customer</b></a></td>
              <td width="150"><a href="logout.php"><b>Logout</b></a></td></tr>
      </table>

        <table border="0">

            <tr><td><label>Agent Name&nbsp;*</label></td>
                <td><input id="agent_name" type=text size='20' value=''></td>
                <td><div id="agent_name_hint"> </div></td></tr>

            <tr><td><label>Agent Code&nbsp;*</label></td>
                <td><input id="agent_code" type=text size='20' value=''></td>
                <td><div id="agent_code_hint"> </div></td></tr>

            <tr><td><label>Centre Code&nbsp;*</label></td>
                <td><select id="centre_code"><option value=''></option></select></td>
                <td><div id="centre_code_hint"> </div></td></tr>

            <tr><td><label>Mobile Phone&nbsp;*</label></td>
                <td><input id="mobile_phone" type=text size='20' value=''></td>
                <td><div id="mobile_phone_hint"> </div></td></tr>

            <tr><td><label>Office Phone&nbsp;*</label></td>
                <td><input id="office_phone" type=text size='20' value=''></td>
                <td><div id="office_phone_hint"> </div></td></tr>

            <tr><td><label>Address</label></td>
                <td><textarea  id=address rows=5 cols=20></textarea></td>
                <td><div id="address_hint"> </div></td></tr>

            <tr><td><label>Password&nbsp;*</label></td>
                <td><input id="password" type=password size='20' value=''></td>
                <td><div id="password_hint"> </div></td></tr>

            <tr><td><label>Confirm Password&nbsp;*</label></td>
                <td><input id="password2" type=password size='20' value=''></td>
                <td><div id="password2_hint"> </div></td></tr>

            <tr /> <tr /> <tr />
        </table>
        <table>
            <tr><td><input type="submit" onClick="saveForm()" value="Save" /></td>
                <td><input type="submit" onClick="resetForm()" value="Reset" /></td>
                <td><input type="submit" onClick="queryForm()" value="Query" /></td>
                <td><input type="submit" onClick="deleteForm()" value="Delete" /></td></tr>

        </table>
        <div id="agent_table">
            <button onclick="displayTable()">Display the agent table</button>
        </div>

  </body>
</html>
